﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Subgurim.Controles;
using CounterServerModel;
using CounterServerModel.Authentication;
using System.Collections.Generic;

namespace SmartCounters
{
	public partial class GMapsMonitor : System.Web.UI.Page
	{
 		protected void Page_Load(object sender, EventArgs e)
		{
            ((MainTemplate)Master).m_PagePermissionFilter = MainTemplate.k_AdminPageFilter;

            GMapM.reset();

            InformationReader reader = new InformationReader();
            UserInfo userInfo = reader.GetUserInformation(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, ((MainTemplate)Master).AuthenticatedLoggedInUserCredentials.ID);

            if ((userInfo.UserPermissions == eUserPermissions.All) || ((int)userInfo.UserPermissions == 4194303))
            {
                AccountInfo user;
                InformationReader infoProvider = new InformationReader();
                List<AccountInfo> accounts = new InformationReader().GetAllAccounts(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials);
                List<Fault> faults = new InformationReader().GetAllOpenFaults(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials);


                GMapM.Key = "AIzaSyARmX9FYK0psugZwDZWZNPYdWKYJ6U-Hmc";
                GMapM.enableHookMouseWheelToZoom = true;

                GIcon icoAccunt = new GIcon("/Icons/Account.ico");
                GIcon icoFault = new GIcon("/Icons/Fault.ico");
                

                GLatLng israel = new GLatLng(31.774469, 35.213242);

                GMapM.setCenter(israel, 8);
                GMapM.enableGoogleBar = false;

                foreach (AccountInfo account in accounts)
                {
                    string LinkReport = "<a class='Button' href=/InformationCenter/UsageInformation.aspx?AccountID=" + account.AccountID.ToString() + ">Account Usage Report</a>";
                    double lat = account.Latitude;
                    double lon = account.Longitude;

                    string strHtmlInfo =
                                      "City:      " + account.City + "<br>" +
                                      "Street:    " + account.Street + "<br>" +
                                      "House:     " + account.House + "<br>" +
                                      "Apartment: " + account.Apartment + "<br>" +
                                      "Phone:     " + account.Phone + "<br><br>" +
                                      LinkReport;
                    string strTooltipInfo = account.City + " " + account.Street + " " + account.House + "/" + account.Apartment;

                    GLatLng Gllng = new GLatLng(lat, lon);

                    GMarkerOptions GmOptions = new GMarkerOptions();

                    GMarker GMker = new GMarker(Gllng, icoAccunt);

                    GMker.options.title = strTooltipInfo;

                    GInfoWindow info = new GInfoWindow(GMker, strHtmlInfo, GListener.Event.click);

                    GMapM.addInfoWindow(info);
                    GMapM.addGMarker(GMker);

                }


                foreach (Fault fault in faults)
                {
                    user = infoProvider.GetAccountInformation(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, fault.AccountID);

                    string LinkFault = "<a class='Button' href=/InformationCenter/FaultStatus.aspx?AccountID=" + user.AccountID.ToString() + ">Fault update</a>";
                    double lat = user.Latitude;
                    double lon = user.Longitude;

                    string strHtmlInfo =
                                      "City:      " + user.City + "<br>" +
                                      "Street:    " + user.Street + "<br>" +
                                      "House:     " + user.House + "<br>" +
                                      "Apartment: " + user.Apartment + "<br>" +
                                      "Phone:     " + user.Phone + "<br><br>" +
                                      LinkFault;

                    string strTooltipInfo = user.City + " " + user.Street + " " + user.House + "/" + user.Apartment;


                    GLatLng Gllng = new GLatLng(lat, lon);

                    GMarkerOptions GmOptions = new GMarkerOptions();

                    GMarker GMker = new GMarker(Gllng, icoFault);

                    GMker.options.title = strTooltipInfo;

                    GInfoWindow info = new GInfoWindow(GMker, strHtmlInfo, GListener.Event.click);

                    GMapM.addInfoWindow(info);
                    GMapM.addGMarker(GMker);

                }
            }


		}

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            Page_Load(null,null);

            GIcon icoArrowDown = new GIcon("/Icons/ArrowDown.ico");
            GeoCode gCode = GMap.geoCodeRequest(TxtSearch.Text, GMapM.Key);

            GLatLng nLatLng = new GLatLng(gCode.Placemark.coordinates.lat,gCode.Placemark.coordinates.lng);
            
            String place = TxtSearch.Text;
            GMarkerOptions gmo = new GMarkerOptions(icoArrowDown, place);
            
            GMapM.GZoom = 16;
            GMapM.mapType = GMapType.GTypes.Normal;

            GMarker GMker = new GMarker(nLatLng, icoArrowDown);
            GInfoWindow info = new GInfoWindow(GMker, place, GListener.Event.click);

            GMarker nMarker = new GMarker(nLatLng, gmo);           
            GMapM.addGMarker(nMarker);
            GMapM.setCenter(nLatLng);
        }
	}
}